<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="sim卡号" prop="simCode">
        <el-input v-model="dataForm.simCode" placeholder="sim卡号"></el-input>
      </el-form-item>
      <el-form-item label="sim手机号" prop="mobile">
        <el-input v-model="dataForm.simMobile" placeholder="sim手机号"></el-input>
      </el-form-item>
      <el-form-item label="激活时间" prop="activedate">
        <!--        <el-input disabled v-model="dataForm.activedate" placeholder="激活时间"></el-input>-->
        <el-date-picker
          v-model="dataForm.activedate"
          type="date"
          placeholder="激活时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期时间" prop="expireDate">
        <el-date-picker
          v-model="dataForm.expireDate"
          type="date"
          placeholder="过期时间">
        </el-date-picker>
        <!--        <el-input v-model="dataForm.expireDate" placeholder="过期时间"></el-input>-->
      </el-form-item>
      <el-form-item label="设备id" prop="deviceId">
        <el-input v-model="dataForm.deviceId" placeholder="设备id"></el-input>
      </el-form-item>
      <!--      <el-form-item label="代理商用户id" prop="agentUserId">-->
      <!--        <el-input v-model="dataForm.agentUserId" placeholder="代理商用户id"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="代理商" prop="agentUserId">
        <el-select style="padding-bottom: 15px;"
                   filterable
                   clearable
                   prop="agentUserId"
                   v-model="dataForm.agentId" placeholder="请选择">
          <el-option
            :key="0"
            label="厂家"
            :value="0">
          </el-option>
          <el-option
            v-for="item in agentData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
const isNum = (rule, value, callback) => {
  const age = /^[0-9]*$/
  if (!age.test(value)) {
    callback(new Error('请选择代理商'))
  } else {
    callback()
  }
}
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        simCode: '',
        activedate: '',
        dayLeftCount: '',
        deviceId: '',
        // agentUserId: '',
        agentId: '',
        expireDate: '',
        simMobile: ''
      },
      agentUserData: [],
      agentData: [],

      dataRule: {
        simCode: [
          {required: true, message: 'sim卡号不能为空', trigger: 'blur'}
        ],
        activedate: [
          {required: true, message: '激活时间不能为空', trigger: 'blur'}
        ],
        expireDate: [
          {required: true, message: '失效时间不能为空', trigger: 'blur'}
        ],
        deviceId: [
          {required: false, message: '设备id不能为空', trigger: 'blur'}
        ],
        agentId: [
          {required: true, message: '代理商不能为空', trigger: 'blur'},
          {validator: isNum, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/simcard/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.simCode = data.simCard.simCode
              this.dataForm.simMobile = data.simCard.mobile
              this.dataForm.activedate = new Date(data.simCard.activedate)
              this.dataForm.expireDate = new Date(data.simCard.expireDate)
              this.dataForm.deviceId = data.simCard.deviceId
              this.dataForm.agentId = data.simCard.agentId
              // this.dataForm.agentUserId = data.simCard.agentUserId
            }
          })
        }
      })
      this.loadAgent()
      // this.loadAgentUser()
    },
    loadAgent () {
      this.$http({
        url: this.$http.adornUrl('/deviceagent/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 100
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.agentData = data.page.list
        } else {
          this.agentData = []
        }
      })
    },
    loadAgentUser () {
      this.$http({
        url: this.$http.adornUrl('/sys/user/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': 1,
          'limit': 100,
          'roleName': '代理商'
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.agentUserData = data.page.list
        } else {
          this.agentUserData = []
        }
      })
    },
    isNumber (val) {
      return typeof val === 'number' && !isNaN(val)
    },
    // 表单提交
    dataFormSubmit () {
      if (!this.isNumber(this.dataForm.agentId)) {
        this.$message.error('请选择代理商')
        return false
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/simcard/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'simCode': this.dataForm.simCode,
              'mobile': this.dataForm.simMobile,
              'activedate': this.dataForm.activedate.getTime(),
              'expireDate': this.dataForm.expireDate.getTime(),
              'dayLeftCount': this.dataForm.dayLeftCount,
              'deviceId': this.dataForm.deviceId,
              'agentId': this.dataForm.agentId
              // 'agentUserId': this.dataForm.agentUserId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
